﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Course.Entity;

public partial class sysadmin_CourseManager_CourseResource :PowerCreator.LMS.Web.Base.BasePage
{
    public Course opCourse = new Course();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索
            ViewState["OrderBy"] = "";  //默认不排序
            ViewState["OrderType"] = " desc ";//排序方式

            BinderCourseState();
            BinderCourseType();
            InitPage();

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_CourseResource_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseName.Text = GetLanageValueByKey("lbl_CourseResource_CourseName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherName.Text = GetLanageValueByKey("lbl_CourseResource_TeacherName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseType.Text = GetLanageValueByKey("lbl_CourseResource_CourseType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CourseState.Text = GetLanageValueByKey("lbl_CourseResource_CourseState", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Search.Text = GetLanageValueByKey("btn_CourseResource_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SoftSearch.Text = GetLanageValueByKey("btn_CourseResource_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AuditingStateNo.Text = GetLanageValueByKey("depratment_attributes_classstate0", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AuditingStateYes.Text = GetLanageValueByKey("TeacherManager_gv_c1", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    protected void BinderCourseType()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("Course_db_d0", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";

        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("Course_db_d1", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "1";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("Course_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "2";

        ListItem li2 = new ListItem();
        li2.Text = GetLanageValueByKey("Course_db_d3", PowerCreator.LMS.Language.LanagetItemType.Page);
        li2.Value = "3";

        ddl_CourseType.Items.Add(li);
        ddl_CourseType.Items.Add(li0);
        ddl_CourseType.Items.Add(li1);
        ddl_CourseType.Items.Add(li2);
    }
    protected void BinderCourseState()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("AddCourseInfo_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("AddCourseInfo_db_d1", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "1";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("Course_db_d0", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "2";

        ddl_CourseState.Items.Add(li1);
        ddl_CourseState.Items.Add(li);
        ddl_CourseState.Items.Add(li0);
    }

    private string GetSearch()
    {
        string searchA = String.Format(" (Course_Id in (Select FkOID from TauthorityUser as a , TauthorityList as b where a. FkAuthorityID = b.FkAuthorityID and FkUserID ={0} and b.authorityType=4) or FkCourseCreatorid={0})", GetLoginEntity.UserID);
        StringBuilder sbSearch = new StringBuilder();
        if (ViewState["SearchType"].ToString() == "1")
        {
            if (!string.IsNullOrEmpty(txt_CourseName.Text))
            {
                sbSearch.Append(string.Format(" CourseName like '%{0}%' and ", txt_CourseName.Text));
            }
            if (!string.IsNullOrEmpty(txt_CourseMostlyTeacherName.Text))
            {
                sbSearch.Append(string.Format(" CourseMostlyTeacherName like '%{0}%' and ", txt_CourseMostlyTeacherName.Text));
            }
            if (ddl_CourseType.SelectedValue.ToString() != "0")
            {
                sbSearch.Append(string.Format(" CourseIsCompulsory = {0} and ", ddl_CourseType.SelectedValue.ToString()));
            }
            if (ddl_CourseState.SelectedValue.ToString() != "2")
            {
                sbSearch.Append(string.Format(" CourseState = {0} and ", ddl_CourseState.SelectedValue.ToString()));
            }
            //如果是超级管理员登录可以查看所有课程
            if (GetLoginEntity.UserName.ToLower() != "admin" && GetLoginEntity.UserName.ToLower() != "administrator")
            {
                sbSearch.Append(searchA);
            }
        }
        else if (ViewState["SearchType"].ToString() == "0")
        {
            //如果是超级管理员登录可以查看所有课程
            if (GetLoginEntity.UserName.ToLower() == "admin" || GetLoginEntity.UserName.ToLower() == "administrator")
            {
                sbSearch.Append(string.Format(" (CourseName like '%{0}%' or CourseMostlyTeacherName like '%{0}%' or Description like '%{0}%') ", txt_SearchKey.Text));
            }
            else
            {
                sbSearch.Append(string.Format(" (CourseName like '%{0}%' and {1} or CourseMostlyTeacherName like '%{0}%' and {1} or Description like '%{0}%' and {1}) ", txt_SearchKey.Text, searchA));

            }
        }
        if (sbSearch.ToString().EndsWith(" and "))
            sbSearch.Remove(sbSearch.Length - 4, 4);

        return sbSearch.ToString();
    }

    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = opCourse.QueryCourseCount(search);
    }
    private void BinderRPT(string search,int pageIndex)
    {
        string orderby = ViewState["OrderBy"].ToString();
        if (!string.IsNullOrEmpty(orderby))
        {
            orderby += ViewState["OrderType"].ToString();
        }
        DataTable dt = opCourse.QueryCourse(search, orderby, LMSPageSize, pageIndex);
        rptCourse.DataSource = dt;
        rptCourse.DataBind();
        PreAspNetPager(AspNetPager1);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rptCourse_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "laddnew")
        {
            //添加新资源
            string url = "AddResourceInfo.aspx?CoursersClass=1&AddResourceType=addnew&CourseID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName == "lupload")
        {
            //上传资源
            string url = "UploadResource.aspx?CourseID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName == "lshow")
        {
            //查看课程资源
            string url = "ResourceList.aspx?CourseID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName == "laddoldresource")
        {
            //引用现有资源
            string url = "ResourceSelect.aspx?CourseID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName == "laddoldpaper")
        {
            //引用试卷
            string url = "PaperSelect.aspx?CourseID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName == "lpl")
        {
            //点评管理
            string url = "ResourceCommentNew.aspx?CourseID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName == "orderby")
        {
            ViewState["OrderBy"] = e.CommandArgument.ToString();
            string search = GetSearch();
            BinderRPT(search,AspNetPager1.CurrentPageIndex);
            if (ViewState["OrderType"].ToString() == " desc")
                ViewState["OrderType"] = " asc";
            else
                ViewState["OrderType"] = " desc";
        }
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选/取消
        for (int i = 0; i < rptCourse.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptCourse.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptCourse.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptCourse.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_AuditingStateYes_Click(object sender, EventArgs e)
    {
        //冻结选中
        for (int i = 0; i < rptCourse.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptCourse.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptCourse.Items[i].FindControl("lblid");
                VOTOCourse vo = opCourse.LoadVOTCourse(int.Parse(lblid.Text));
                if (vo != null)
                {
                    if (vo.CourseState != 1)
                    {
                        vo.CourseState = 1;
                        opCourse.UpdateCourse(vo);
                    }
                }
            }
        }
        chkAll.Checked = false;
        chkCancel.Checked = false;
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_AuditingStateNo_Click(object sender, EventArgs e)
    {
        //冻结选中
        for (int i = 0; i < rptCourse.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptCourse.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptCourse.Items[i].FindControl("lblid");
                VOTOCourse vo = opCourse.LoadVOTCourse(int.Parse(lblid.Text));
                if (vo != null)
                {
                    if (vo.CourseState != 0)
                    {
                        vo.CourseState = 0;
                        opCourse.UpdateCourse(vo);
                    }
                }
            }
        }
        chkAll.Checked = false;
        chkCancel.Checked = false;
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_SoftSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
}
